import numpy as np, scipy.special as sp, scipy.integrate as ig, copy, re, math
import sys
from Model import Model,Actor,Evidence,TrackSegment,Node,TimeNode,Time,Distributions

class Standstill:

    def __init__(self):
        self.model = Model()
        self.model.name = "Standstill"
        self.model.subscriptions = [""]
        self.model.topNode = "standstillmain"
        self.model.independent = True

        act1 = Actor()
        act1.name = "dummy1"
        act1.atype = Actor.NULL

        tempNode = Node()
        tempNode.getID()
        tempNode.name = "standstillmain"
        tempNode.subjects = [act1]
        tempNode.objects = []
        tempNode.evvecs = []
        tempNode.bound = False

        self.model.topNode = tempNode.name
        self.model.nodes.append(copy.deepcopy(tempNode))

        tempNode.name = "posstable"
        tempEv = TrackSegment()
        tempEv.actor = act1
        tempEv.segtype = Evidence.NOT_MOVE
        tempEv.willBind = True
        tempEv.endsNode = True
        tempEv.bound = False

        tempNode.evvecs = [copy.deepcopy(tempEv)]

        def standstillJoint(evvecs):
            track = evvecs[0]
            avgDistanceFromCenter = track.calculate_distance_from_center()
            print "calculated distance from center:",avgDistanceFromCenter
            distanceWeight = Standstill.avgDistanceDensity(avgDistanceFromCenter)
            print "weight:",distanceWeight
            time = (track.endtime - track.starttime)/Model.FR/60.
            print "time:",time
            timeWeight = Standstill.timeDensity(time)
            print "time weight:",timeWeight
            totalWeight = timeWeight + distanceWeight
            return totalWeight,.5

        tempNode.lfunction = standstillJoint

        self.model.nodes.append(copy.deepcopy(tempNode))

    @staticmethod
    def avgDistanceDensity(avgDistance):
        weight = Distributions.PDFs.foldednorm(avgDistance,0.02,0.5)
        return weight

    @staticmethod
    def timeDensity(time):
        #loitering time is in minutes
        weight = Distributions.PDFs.gamma(time,0.05,0.75)
        return weight
